package cate.game.farm.local.forbidden;

import cate.common.table.d.GDForbidden;
import cate.common.table.d.GDFunc;
import cate.common.table.d.GDMail;
import cate.common.table.d.GDOperation;
import cate.common.table.farm.forbidden.row.ForbiddenRankRewardRow;
import cate.common.table.farm.forbidden.row.ForbiddenRoomRow;
import cate.game.GameBody;
import cate.game.db.mongo.kv.KvMongo;
import cate.game.mail.po.MailBuilder;
import cate.game.rank.h.ForbiddenRankHandler;
import cate.game.rank.po.Rank;
import cate.game.res.MixRes;
import cate.game.role.farm.forbidden.ForbiddenRoom;
import cate.game.role.farm.forbidden.msg.ForbiddenAllMonsterResp;
import cate.game.role.mail.Mail;
import com.fasterxml.jackson.annotation.JsonIgnore;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.annotation.Transient;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@ToString
@NoteClass("种族禁地系统状态")
public class ForbiddenSPO extends KvMongo {

    @Transient
    @JsonIgnore
    transient GameBody gameBody;

    @NoteField(value = "轮次")
    public int round;

    @Override
    public String getKeyPrefix() {
        return KvMongo.KEY_FARM_FORBIDDEN;
    }

    @NoteField(value = "种族禁地的各圣殿数据")
    public List<ForbiddenRoom> roomList;

    public void initialize(GameBody game) {
        this.gameBody = game;
        if (roomList == null) {
            roomList = new ArrayList<>();
        }
        for (ForbiddenRoomRow tpl : game.table.forbidden.room.getList()) {
            getRoom(tpl.roomId);
        }
        roomList.forEach(room -> room.initialize(game));
    }

    @JsonIgnore
    @Transient
    public ForbiddenRoom getRoom(int roomId) {
        for (ForbiddenRoom room : roomList) {
            if (room.id == roomId) {
                return room;
            }
        }
        ForbiddenRoom room = new ForbiddenRoom(roomId, gameBody);
        roomList.add(room);
        return room;
    }

    public void onDaySpan() {
        for (ForbiddenRoom room : roomList) {
            room.onDaySpan();
        }
    }

    public void onWeekSpan() {
        round++;//0,1,2
        for (ForbiddenRoom room : roomList) {
            room.onWeekSpan();
            room.resetRound(round);
        }
        noticeUpAll();
    }

    private void noticeUpAll() {
        gameBody.role.activeForOnline(role -> {
            role.sendNow(new ForbiddenAllMonsterResp(role));
        });
    }

    public synchronized void handleReward() {
        ForbiddenRankHandler handler = gameBody.rank.forbidden.handlers.get(GDForbidden.Type.TOTAL.getType());
        List<Rank> ranks = handler.getRankList(null);
        for (int i = 0; i < ranks.size(); ++i) {
            int idx = i + 1;
            MixRes reward = null;
            for (ForbiddenRankRewardRow row : gameBody.table.forbidden.rankReward.getList()) {
                if (idx >= row.begin && idx <= row.end) {
                    reward = new MixRes(row.rewardStr);
                    break;
                }
            }
            Rank rank = ranks.get(i);
            if (reward != null) {
                List<Object> params = new ArrayList<>();
                params.add(idx);
                Mail mail = new MailBuilder()
                        .setContentId(GDMail.Tid.FORBIDDEN_RANK_REWARD)
                        .setParamList(params)
                        .setReward(reward)
                        .setOperation(GDOperation.FORBIDDEN_RANK)
                        .setOperationSub(idx)
                        .build();
                gameBody.mail.send(rank.owner.uid, mail);
            }
            log.info("种族禁地排行榜第{}名是{}", idx, rank.owner.base.name);
            //清空排行榜
        }
        handler.clearList();
    }
}
